/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mainpackage;

import function.QuickOption;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Enumeration;
import javax.microedition.lcdui.Image;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import ui.UiEquipment;
import ui.UiInterFace;
import ui.UiLogin;

/**
 *
 * @author Administrator
 */
public class Rms {
    /*
     * 游戏记录存储
     */

    public static void saveGame(byte byRmsName, byte[] data) {
        RecordStore rms = null;
        String strRmsName = null;
        Tools.printLog("开始保存游戏" + byRmsName);
        try {
            strRmsName = String.valueOf(byRmsName);
            //用户RMS设置
            rms = RecordStore.openRecordStore(strRmsName, true);
            ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
            DataOutputStream dataIS = new DataOutputStream(byteArrayOS);
            switch (byRmsName) {
                case Constant.RMS_LOGIN_USENAME_PASSWORD:
                    dataIS.writeUTF(GameBean.STR_CMD_LOGIN_USENAME);
                    dataIS.writeUTF(GameBean.STR_CMD_LOGIN_PASSWORD);
                    byte[] gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    gameData = null;
                    break;
                case Constant.RMS_MAP_TER_DATA:
                    //用户RMS设置
                    gameData = data;//gameMap.saveMap().toByteArray();
                    if (rms.getNumRecords() == 0) {
//                        Tools.printLog("无记录添加" + byRmsName);
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        dataIS.writeUTF(MainLogic.getInstance().uiInterFace.sRoamMapKeys);
                        dataIS.write(data, 0, data.length);
                        gameData = byteArrayOS.toByteArray();
                        rms.addRecord(gameData, 0, gameData.length);
//                        Tools.printLog("有记录添加" + byRmsName);
                    }
                    gameData = null;
                    break;
                case Constant.RMS_ONE_ROOM_IMAGE:
                    String imgId;
                    int shImgLen = 0;
                    int[] iImgData = new int[1];
                    int imgIdarray = 0;
                    int j = 0;
                    Image aImg;
                    Enumeration keys = ResManager.publicOneRoomResImgs.keys();
                    dataIS.writeUTF(MainLogic.getInstance().uiInterFace.sRoamMapKeys);
                    dataIS.writeInt(ResManager.publicOneRoomResImgs.size());
                    Tools.printLog("存储图片数量：" + ResManager.publicOneRoomResImgs.size());
                    Tools.printLog("存储图片key数量：" + keys.hasMoreElements());
                    while (keys.hasMoreElements()) {
                        imgId = (String) keys.nextElement();
                        Tools.printLog("存储图片ID：" + imgId);
                        aImg = ((Image) (ResManager.publicOneRoomResImgs.get(imgId)));
                        iImgData = new int[aImg.getWidth() * aImg.getHeight()];
                        imgIdarray = Integer.parseInt(imgId);
                        try {
                            // aImg.getRGB(iImgData, 0, iImgData.length, 0, 0, aImg.getWidth(), aImg.getHeight());
                            aImg.getRGB(iImgData, 0, aImg.getWidth(), 0, 0, aImg.getWidth(), aImg.getHeight());
                        } catch (Exception e) {
                            if (Constant.DEBUG) {
                                e.printStackTrace();
                            }
                        }
                        dataIS.writeUTF(imgId);
                        dataIS.writeInt(iImgData.length);
                        for (j = 0; j < iImgData.length; j++) {
                            dataIS.writeInt(iImgData[j]);
                        }
                    }
                    aImg = null;
                    imgId = null;
                    iImgData = null;
                    break;
                case Constant.RMS_GAME_SET:
                    gameData = data;//gameMap.saveMap().toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        dataIS.write(data, 0, data.length);
                        gameData = byteArrayOS.toByteArray();
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    Tools.printLog("保存设置" + byRmsName);
                    gameData = null;
                    break;
                case Constant.RMS_CONNECT_IP:
                    dataIS.writeUTF(Constant.CONNECT_IP);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_PLAYER_LEVLE:
                    dataIS.writeInt(GameBean.L_CMD_PLAYER_OWN_LEVLE);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_PLAYER_SEX:
                    dataIS.writeByte(GameBean.L_CMD_PLAYER_OWN_SEX);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_PLAYER_ID:
                    dataIS.writeLong(GameBean.L_CMD_PLAYER_OWN_ID);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_PLAYER_INDEX:
                    dataIS.writeByte(Constant.PLAYER_INDEX);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_NEW_PLAYER_TUITION_1:
                    dataIS.writeInt(MainLogic.getInstance().uiInterFace.sysMessage.saveStateGuide);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_NEW_PLAYER_TUITION_2:
                    dataIS.writeInt(MainLogic.getInstance().uiInterFace.sysMessage.saveStateGuide);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_NEW_PLAYER_TUITION_3:
                    dataIS.writeInt(MainLogic.getInstance().uiInterFace.sysMessage.saveStateGuide);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_NEW_PLAYER_TUITION_4:
                    dataIS.writeInt(MainLogic.getInstance().uiInterFace.sysMessage.saveStateGuide);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_GAME_DRAW_SET:
                    for (int i = 0; i < UiEquipment.isopenset.length; i++) {
                        dataIS.writeBoolean(UiEquipment.isopenset[i]);
                    }
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_NEW_PLAYER_CAN_GO_LOCK:
                    dataIS.writeLong(MainLogic.getInstance().gamePlayer.lPlayerID);
                    dataIS.writeBoolean(GameBean.BL_NEWPLAYER_CAN_GO_LOCK);
                    Tools.printLog("rmsBL_NEWPLAYER_CAN_GO_LOCK" + GameBean.BL_NEWPLAYER_CAN_GO_LOCK);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_PLAYER_NETWORK:
                    dataIS.writeByte(1);
//                    dataIS.writeByte(GameBean.BY_NET_CONNECT);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_SOCKET_AUTHCODE1:
                    dataIS.writeInt(Constant.AUTH_CODE);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_SOCKET_AUTHCODE2:
                    dataIS.writeInt(Constant.AUTH_CODE2);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_SERVER_ID:
                    dataIS.writeInt(GameBean.SERVER_ID);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_QUICK_STATUS:
                    dataIS.writeByte(GameBean.BY_QUICK_ENTER);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_SUPER_PASSWD:
                    dataIS.writeUTF(GameBean.STR_CMD_SUPER_PASSWORD);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
                case Constant.RMS_MOBILE_IMEI:
                    dataIS.writeUTF(GameBean.STR_MOBILE_IMEI);
                    gameData = byteArrayOS.toByteArray();
                    if (rms.getNumRecords() == 0) {
                        rms.addRecord(gameData, 0, gameData.length);
                    } else {
                        rms.closeRecordStore();
                        RecordStore.deleteRecordStore(strRmsName);
                        rms = RecordStore.openRecordStore(strRmsName, true);
                        rms.addRecord(gameData, 0, gameData.length);
                    }
                    break;
            }
            if (rms != null) {
                rms.closeRecordStore();
            }
            byteArrayOS = null;
            dataIS = null;
            rms = null;
            strRmsName = null;
            data = null;
        } catch (Exception ex) {
            if (Constant.DEBUG) {
                ex.printStackTrace();
            }
        } finally {
            try {
                if (rms != null) {
                    rms.closeRecordStore();
                }
            } catch (RecordStoreNotOpenException ex1) {
                if (Constant.DEBUG) {
                    ex1.printStackTrace();
                }
            } catch (RecordStoreException ex1) {
                if (Constant.DEBUG) {
                    ex1.printStackTrace();
                }

            }
            rms = null;
            data = null;
            strRmsName = null;
        }
    }

    public static boolean loadGame(byte byRmsName) {
        RecordStore rms = null;
        String strRmsName = null;
        Tools.printLog("载入游戏" + byRmsName);
        boolean blHaveData = true;
        try {
            strRmsName = String.valueOf(byRmsName);
            rms = RecordStore.openRecordStore(strRmsName, true);
            if (rms.getNumRecords() == 0) {
                if (rms != null) {
                    rms.closeRecordStore();
                }
                rms = null;
                Tools.printLog("无载入游戏记录" + byRmsName);
                return false;
            }
            byte[] byGameData = rms.getRecord(1);
            ByteArrayInputStream byteArrayIS = new ByteArrayInputStream(byGameData);
            DataInputStream dataIS = new DataInputStream(byteArrayIS);

            switch (byRmsName) {
                //登录用户名+密码
                case Constant.RMS_LOGIN_USENAME_PASSWORD:
                    GameBean.STR_CMD_LOGIN_USENAME = dataIS.readUTF();
                    GameBean.STR_CMD_LOGIN_PASSWORD = dataIS.readUTF();
                    break;
                case Constant.RMS_MAP_TER_DATA:
                    //读取map 的场景key 如果是本场景则读取并返回true否则返回false
                    if (MainLogic.getInstance().uiInterFace.sRoamMapKeys.equals(dataIS.readUTF())) {
                        //读图
                        //读动画
                        //读地图
                        MainLogic.getInstance().gameMap.initMapData(dataIS, false);
                    } else {
                        blHaveData = false;
                    }
                    break;
                //存储npc和地图图片
                case Constant.RMS_ONE_ROOM_IMAGE:
                    String imgId = null;
                    byte[] byImgData = null;
                    int length = 0;
                    if (MainLogic.getInstance().uiInterFace.sRoamMapKeys.equals(dataIS.readUTF())) {
                        int imgNums = dataIS.readInt();
//                        System.out.print("载入存储图片数量：" + imgNums);
                        for (int j = 0; j < imgNums; j++) {
                            imgId = dataIS.readUTF();
                            length = dataIS.readInt();
                            byImgData = new byte[length];
                            dataIS.read(byGameData);
                            ResManager.createImage(Integer.parseInt(imgId), Constant.RES_PUBLIC_ONEROOM, byImgData);
                            Tools.printLog("载入存储的图片ID:" + imgId);
                        }
                    } else {
                        blHaveData = false;
                    }
                    byImgData = null;
                    break;
                case Constant.RMS_GAME_SET:
                    break;
                case Constant.RMS_PLAYER_LEVLE:
                    GameBean.L_CMD_PLAYER_OWN_LEVLE = dataIS.readInt();
                    break;
                case Constant.RMS_PLAYER_SEX:
                    GameBean.L_CMD_PLAYER_OWN_SEX = dataIS.readByte();
                    break;
                case Constant.RMS_PLAYER_ID:
                    GameBean.L_CMD_PLAYER_OWN_ID = dataIS.readLong();
                    break;
                case Constant.RMS_CONNECT_IP:
//                    Constant.CONNECT_IP = 
                    	dataIS.readUTF();
                    break;
                case Constant.RMS_PLAYER_INDEX:
                    Constant.PLAYER_INDEX = dataIS.readByte();
                    break;
                case Constant.RMS_NEW_PLAYER_TUITION_1:
                    MainLogic.getInstance().uiInterFace.sysMessage.saveStateGuide = dataIS.readInt();
                    break;
                case Constant.RMS_NEW_PLAYER_TUITION_2:
                    MainLogic.getInstance().uiInterFace.sysMessage.saveStateGuide = dataIS.readInt();
                    break;
                case Constant.RMS_NEW_PLAYER_TUITION_3:
                    MainLogic.getInstance().uiInterFace.sysMessage.saveStateGuide = dataIS.readInt();
                    break;
                case Constant.RMS_NEW_PLAYER_TUITION_4:
                    MainLogic.getInstance().uiInterFace.sysMessage.saveStateGuide = dataIS.readInt();
                    break;
                case Constant.RMS_GAME_DRAW_SET:
                    for (int i = 0; i < UiEquipment.isopenset.length; i++) {
                        UiEquipment.isopenset[i] = dataIS.readBoolean();
                    }
                    break;
                case Constant.RMS_NEW_PLAYER_CAN_GO_LOCK:
                    if (MainLogic.getInstance().gamePlayer.lPlayerID == dataIS.readLong()) {
                        GameBean.BL_NEWPLAYER_CAN_GO_LOCK = dataIS.readBoolean();
                    } else {
                        GameBean.BL_NEWPLAYER_CAN_GO_LOCK = true;
                    }
                    break;
                case Constant.RMS_PLAYER_NETWORK:
                    dataIS.readByte();
                    break;
                case Constant.RMS_SOCKET_AUTHCODE1:
                    Constant.AUTH_CODE = dataIS.readInt();
                    break;
                case Constant.RMS_SOCKET_AUTHCODE2:
                    Constant.AUTH_CODE2 = dataIS.readInt();
                    break;
                case Constant.RMS_SERVER_ID:
                    GameBean.SERVER_ID = dataIS.readInt();
                    break;
                case Constant.RMS_QUICK_STATUS:
                    GameBean.BY_QUICK_ENTER = dataIS.readByte();
                    break;
                case Constant.RMS_SUPER_PASSWD:
                    GameBean.STR_CMD_SUPER_PASSWORD = dataIS.readUTF();
                    break;
                case Constant.RMS_MOBILE_IMEI:
                    GameBean.STR_MOBILE_IMEI = dataIS.readUTF();
                    break;
            }
            if (rms != null) {
                rms.closeRecordStore();
            }
            dataIS.close();
            byteArrayIS.close();
            dataIS = null;
            byteArrayIS = null;
            byGameData = null;
            rms = null;
            strRmsName = null;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            try {
                if (rms != null) {
                    rms.closeRecordStore();
                }
                rms = null;
                strRmsName = null;
            } catch (RecordStoreNotOpenException ex1) {
                ex1.printStackTrace();
            } catch (RecordStoreException ex1) {
                ex1.printStackTrace();
            }
        }
        return blHaveData;
    }
}
